﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using PanasonicWarranty.Business;
using System.Web.UI.HtmlControls;
using Utils;
using System.IO;
using Settings;
using System.Data;
namespace PanasonicWarranty.UI
{
    public partial class CategoryManagement : BasePageManageWarranty
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindGridData();
            pagingtop.Visible = pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                pagingtop.PageDisplay = pagingbottom.PageDisplay = 6;
                BindGridData();
            }
        }

       
        private void BindGridData()
        {
            try
            {
                string code = txtSearchCode.Text.Trim();
                string name = "";
                bool? disable = ddlSearchDisabled.SelectedValue == string.Empty ? 
                                (bool?)null : Convert.ToBoolean(ddlSearchDisabled.SelectedValue);
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRow = 0;
                grvData.DataSource = pa_Category.Paging(currentPage, pageSize, ref totalRow, code, name, disable);
                pagingtop.TotalRecord = pagingbottom.TotalRecord = totalRow;
                pagingtop.Visible = pagingbottom.Visible = pagingtop.TotalPage > 1;
                grvData.DataBind();
                ltrInfo.Text = "Tổng cộng: " + totalRow; 
            }
            catch { }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
            BindGridData();
        }

        protected void tSortable_length_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                BindGridData();
            }
            catch { }
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                string cateCode = (string)e.CommandArgument;
                hdfID.Value = cateCode;
                switch (e.CommandName)
                {
                    case "Editing":
                        var obj = DB.pa_Categories.FirstOrDefault(p => p.CateCode == cateCode);
                        if (obj != null)
                        {
                            txtCateCode.Enabled = false;
                            txtCateCode.Text = obj.CateCode;
                            txtCateName.Text = obj.CateName;
                            txtDescription.Text = obj.Description;
                            chkDisable.Checked = obj.Disabled ?? false;
                            RegisterStartUpScript("OpenDialog('dialog1',400);");
                        }
                        break;
                    case "Deleting":
                        Authorize(Role.Write);
                        obj = DB.pa_Categories.FirstOrDefault(p => p.CateCode == cateCode);
                        if (obj != null)
                        {
                            DB.pa_Categories.DeleteOnSubmit(obj);
                            DB.SubmitChanges();
                            //Ghi log dữ liệu
                            InsertChangeLog("Xóa dữ liệu loại sản phẩm");
                            BindGridData();
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                pa_Category obj;
                if (hdfID.Value == "")//Add new
                {
                    obj = new pa_Category();
                    obj.CateCode = txtCateCode.Text;
                }
                else //Edit
                {
                    obj = DB.pa_Categories.FirstOrDefault(p => p.CateCode == hdfID.Value);
                }
                obj.CateName = txtCateName.Text;
                obj.Description = txtDescription.Text;
                obj.Disabled = chkDisable.Checked;
                //Save
                if (hdfID.Value == "")
                    DB.pa_Categories.InsertOnSubmit(obj);
                DB.SubmitChanges();
                //Ghi log dữ liệu
                InsertChangeLog(hdfID.Value == "" ? "Thêm mới dữ liệu loại sản phẩm" : "Cập nhật dữ liệu loại sản phẩm");
                BindGridData();
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void lbtnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                ControlClass.ResetControlValues(pnlDialogBlock);
                txtCateCode.Enabled = true;
                RegisterStartUpScript("OpenDialog('dialog1',400);");
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void btnDownload_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Download);
                DownloadFile_Tracking(Server.MapPath(string.Format(@"~/App_Data/{0}", AppSetting.PatternFileUploadProductCate)));
            }
            catch (Exception ex)
            {
                ShowMessageBox(ex.Message);
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Export);
                using (DataTable dt = pa_Category.GetAll(null))
                {
                    if (dt.Rows.Count > 0)
                    {
                        //Transform table
                        foreach (DataColumn col in dt.Columns)
                        {
                            col.Caption = pa_Category.GetCaption(col.ColumnName);
                        }
                        ExportToExcel(dt, "ExportLoaiSanPham.xlsx");
                    }
                    else
                    {
                        ShowMessageBox("Không có dữ liệu export");
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
    }
}